package com.getpebble.android.core;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.fragment.IHandleBackPress;

public abstract class PblBaseActivity extends Activity {
    protected String TAG = PblBaseActivity.class.getSimpleName();
    private ActionBar mActionBar = null;
    private FragmentManager mFragmentManager = null;
    private boolean mIsDestroyed = false;

    public void onCreate(Bundle savedInstanceState) {
        this.TAG = getClass().getSimpleName();
        Trace.verbose(this.TAG, "onCreate(" + savedInstanceState + ")");
        this.mActionBar = getActionBar();
        this.mFragmentManager = getFragmentManager();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pbl_base);
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    protected void setActionBarIcon(int resourceId) {
        if (this.mActionBar != null) {
            this.mActionBar.setIcon(resourceId);
        }
    }

    protected void hideActionBar() {
        if (this.mActionBar != null) {
            this.mActionBar.hide();
        }
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 16908332:
                onBackPressed();
                break;
            default:
                Trace.debug(this.TAG, "Unhandled options item selected");
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    protected final int switchFragment(Fragment fragment) {
        Trace.verbose(this.TAG, "switchFragment(" + fragment + ")");
        return switchFragment(fragment, false, false, false);
    }

    public final int switchFragment(Fragment fragment, boolean allowSameFragment, boolean force, boolean addToBackStack) {
        Trace.verbose(this.TAG, "switchFragment(" + fragment + ", " + allowSameFragment + ", " + addToBackStack + ")");
        Fragment currentFragment = getCurrentFragment();
        if (allowSameFragment || currentFragment == null || !currentFragment.getClass().getName().equals(fragment.getClass().getName())) {
            FragmentTransaction transaction = this.mFragmentManager.beginTransaction();
            transaction.replace(R.id.grp_fragment_container, fragment);
            if (addToBackStack && transaction.isAddToBackStackAllowed()) {
                transaction.addToBackStack(fragment.getClass().getName());
            }
            int commitId = transaction.commit();
            if (!force) {
                return commitId;
            }
            this.mFragmentManager.executePendingTransactions();
            return commitId;
        }
        Trace.info(this.TAG, "Cannot switch, same fragment and 'allowSameFragment' is false!");
        return -1;
    }

    protected final Fragment getCurrentFragment() {
        Fragment fragment = this.mFragmentManager != null ? this.mFragmentManager.findFragmentById(R.id.grp_fragment_container) : null;
        Trace.verbose(this.TAG, "getCurrentFragment()::Fragment[" + fragment + "]");
        return fragment;
    }

    public void onStart() {
        Trace.verbose(this.TAG, "onStart()");
        super.onStart();
    }

    public void onResume() {
        Trace.verbose(this.TAG, "onResume()");
        super.onResume();
    }

    public void onPause() {
        Trace.verbose(this.TAG, "onPause()");
        super.onPause();
    }

    public void onStop() {
        Trace.verbose(this.TAG, "onStop()");
        super.onStop();
    }

    public void onDestroy() {
        Trace.verbose(this.TAG, "onDestroy()");
        super.onDestroy();
        this.mIsDestroyed = true;
    }

    protected boolean isActivityDestroyed() {
        return this.mIsDestroyed;
    }

    public void onSaveInstanceState(Bundle outState) {
        Trace.verbose(this.TAG, "onSaveInstanceState(" + outState + ")");
        super.onSaveInstanceState(outState);
    }

    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }

    public void onBackPressed() {
        Fragment currentFragment = getCurrentFragment();
        if (currentFragment != null && (currentFragment instanceof IHandleBackPress) && ((IHandleBackPress) currentFragment).canGoBack()) {
            ((IHandleBackPress) currentFragment).goBack();
        } else {
            super.onBackPressed();
        }
    }

    public SharedPreferences getSharedPreferences(String name, int mode) {
        return getApplicationContext().getSharedPreferences(name, mode);
    }
}
